initStorage();
var startSearch = false;

chrome.storage.onChanged.addListener(function(changes, namespace) {
	if ((namespace === 'local') && ('getTextMethod' in changes)) {
		chrome.storage.local.get('getTextMethod', function (result) {
			window.getTextMethod = result.getTextMethod;
		});
	}
});


var styleElement = document.createElement('style');
styleElement.textContent = '@font-face {' +
	'font-family: "yourDictFontAwesome";' +
	'src: url("' + chrome.extension.getURL("lib/fontawesome-webfont.ttf") + '") format("truetype");' +
	'font-weight: normal;' +
	'font-style: normal;' +
	'}';
document.body.appendChild(styleElement);


var mousePosition = {x: 0, y: 0, pageX: 0, pageY: 0};

document.addEventListener('dblclick', function(event){
	if (getTextMethod.dblclick === false) {
		return;
	}
	getSelectAndPopup(event);
}, true);
document.addEventListener('mousedown', function (event) {
	removeYourDict();
	mousePosition.x = event.clientX;
	mousePosition.y = event.clientY;
});
document.addEventListener('mouseup', function (event) {
	if (getTextMethod.stroke === false) {
		return;
	}
	if ((Math.abs(event.clientX - mousePosition.x) > 5) ||
			Math.abs(event.clientY - mousePosition.y) > 3) {
		getSelectAndPopup(event);
	}
});


function removeYourDict() {
	var yourDict = document.querySelector('.yourDict'),
		yourDictAskForPopup = document.querySelector('.yourDictAskForPopup');
	[yourDict, yourDictAskForPopup].forEach(function (element) {
		if(element){
			document.body.removeChild(element);
		}
	});
}

						  
function getSelectAndPopup(event, text) {
	var text = text || document.getSelection().toString().trim();
	if (text) {
		if (!(/^[0-9a-zA-Z., \-':!();"]+$/.test(text))) {
			return;
		}
		if (!startSearch){
			doAskForPopup(event, text);
			return;
		}
		mousePosition.pageX = event.pageX;
		mousePosition.pageY = event.pageY;
		httpRequest('https://dict.youdao.com/fsearch?q=' + encodeURIComponent(text) + '&xmlVersion=3.2&pos=-1&scrfrom=stroke', 'xml')
			.then(parseYodaoXml)
			.then(doPopup)
			.catch(function (error) {
				console.error(error);
			});
	}
}
	
function doAskForPopup(event, text) {
	var yourDictAskForPopup = document.createElement('div');
	yourDictAskForPopup.className = 'yourDictAskForPopup';
	yourDictAskForPopup.style.top = (event.pageY - 30) + 'px';
	yourDictAskForPopup.style.left = (event.pageX + 15) + 'px';
	yourDictAskForPopup.addEventListener('mousedown', function (e) {
		startSearch = true;
		getSelectAndPopup(event, text);
		e.stopPropagation();
	});
	document.body.appendChild(yourDictAskForPopup);
}
	
function doPopup (parsedObj) {
	var yourDictWrapper = document.createElement('div');
	yourDictWrapper.className = 'yourDict';
	yourDictWrapper.style.top = (mousePosition.pageY + 20) + 'px';
	if (mousePosition.pageX + 350 + 15 > document.documentElement.clientWidth) {
		yourDictWrapper.style.left = document.documentElement.clientWidth - 15 -350 + 'px';
	} else if (mousePosition.pageX < 175 + 15) {
		yourDictWrapper.style.left = '15px';
	} else {
		yourDictWrapper.style.left = mousePosition.pageX - 175 + 'px';
	}
	yourDictWrapper.innerHTML = '<div class="yourDictHead"><div class="yourDictTitle">' + parsedObj.title +
		'</div><div class="yourDictCloseIcon"></div>' + '</div>' +
		'<div class="yourDictMain">' + parsedObj.parsedHTML + '</div>';

	/*在框外点击鼠标时框消失*/
	yourDictWrapper.addEventListener('mousedown', function (event) {
		event.stopPropagation();
	});


	/*关闭按钮*/
	yourDictWrapper.closeIcon = yourDictWrapper.querySelector('.yourDictCloseIcon');
	yourDictWrapper.closeIcon.addEventListener('click', function (event) {
		event.stopPropagation();
		document.body.removeChild(document.querySelector('.yourDict'));
	});
	yourDictWrapper.closeIcon.addEventListener('mousedown', function (event) {
		event.stopPropagation();
	});

	/*拖动查询框*/
	function onMouseMoveEvent(event) {
		var yourDict = document.querySelector('.yourDict');
		if (yourDict === null) {
			document.removeEventListener('mousemove', onMouseMoveEvent);
			return;
		} else {
			yourDict.style.cursor = 'move';
			yourDict.style.top = (event.pageY - mousePosition.y) + 'px';
			yourDict.style.left = (event.pageX - mousePosition.x) + 'px';
		}
	}
	yourDictWrapper.querySelector('.yourDictHead').addEventListener('mousedown', function (event) {
		event.stopPropagation();

		mousePosition.x = event.layerX;
		mousePosition.y = event.layerY;
		document.addEventListener('mousemove', onMouseMoveEvent);
	});
	yourDictWrapper.addEventListener('mouseup', function (event) {
		event.stopPropagation();

		document.querySelector('.yourDict').style.cursor = 'default';
		document.removeEventListener('mousemove', onMouseMoveEvent);
	});

	getYodaoSpeech(parsedObj, yourDictWrapper);
	removeYourDict();
	document.body.appendChild(yourDictWrapper);
}


	

	